/**
 * 
 */
package my.authentication.ldap;

import java.util.ArrayList;
import java.util.List;

import javax.naming.directory.Attributes;

import my.webservices.beans.UserDetails;

import org.springframework.ldap.NamingException;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;

/**
 * @author Seema Khattar
 *
 */
public class Authentication {
	private LdapTemplate ldapTemplate;

	/**
	 * @param ldapTemplate the ldapTemplate to set
	 */
	public void setLdapTemplate(LdapTemplate ldapTemplate) {
		this.ldapTemplate = ldapTemplate;
	} 
	
	public List<UserDetails> checkAuthenticity(final UserDetails userDetail){
		AndFilter filter = new AndFilter();
		filter.and(new EqualsFilter("objectclass", "person"));
//		filter.and(new EqualsFilter("uid", userDetail.getId()));
        return ldapTemplate.search("", filter.encode(),
                new AttributesMapper() {
        	List<UserDetails> userDetails = new ArrayList<UserDetails>();
                    public Object mapFromAttributes(Attributes attrs)
                            throws NamingException {
                        try {
                        	
							String password= (String) attrs.get("userPassword").get();
							UserDetails usrDetail = new UserDetails();
//							usrDetail.setId(userDetail.getId());
//							usrDetail.setpassword
							userDetails.add(usrDetail);
						} catch (javax.naming.NamingException e) {
							e.printStackTrace();
						}
						return userDetails;
                    }
                });

	}
}
